/**
 * 自定义函数
 * */


var formList = {
    data: [],
    pages: {start: 0, limit: 10}
}; //数据列表
var formArys = {
    num: 0
}; //表单数据
var isUse = 0; //0所有，1未使用，2已使用


//列表
function getIndex(v){
    var formData = {
        aid: getAid(),
        isUse: v,
        pageStart: formList.pages.start,
        pageLimit: formList.pages.limit
    };
    console.log(formData);
    $.ajax({
        type: apis.qrIndex.method,
        url: domain + apis.qrIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                if (formList.pages.start > 0) {
                    msgPop({msg: res.msg});
                }
                return
            }
            var d = res.data.data;
            var total = res.data.total;
            for (var i = 0; i < d.length; i ++) {
                formList.data.push(d[i]);
            }
            formList.pages.start ++;
            getHtml();
            $('#total').html(total);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//列表字符串
function getHtml(){
    var h = '', i, d = formList.data, qrlink;
    h += '';
    for (i = 0; i < d.length; i ++) {
        //qrlink = d[i].view!='' ? domainH5 + d[i].view : domainH5 + '?code=' + d[i].code;
        qrlink = domainH5 + '?code=' + d[i].code;
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].code + '</td>';
        h += '<td>' + d[i].show_code + '</td>';
        if (d[i].view == '') {
            h += '<td>未使用</td>';
        } else {
            h += '<td>' + d[i].view + '</td>';
        }
        h += '<td class="td-manage">';
        //h += '<a href="javascript:;" title="编辑" class="ml-5">';
        //h += ' 编辑<i class="Hui-iconfont">&#xe6df;</i>';
        //h += '</a>';
        h += '<a href="javascript:;" class="ml-5" title="点击显示二维码：' + qrlink + '" onclick="popQr(' + d[i].id + ');">';
        h += '显示二维码';
        h += '<input type="hidden" id="qrlink-' + d[i].id + '" value="' + qrlink + '"/>';
        h += '<input type="hidden" id="showCode-' + d[i].id + '" value="' + d[i].show_code + '"/>';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    h += '';
    $('#formList').html(h);
    $('#tableMore').show();
}


//表单新增
function viewAdd(){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var h = '';
    h += '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.6) + 'px; text-align: center; background: #fff; color: #333; border-radius: 5px; position: fixed; top: 5%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">';
    h += '<div style="text-align: center; font-weight: bolder; font-size: 20px;">新增</div>';
    h += '<form class="form form-horizontal" style="padding: 20px; max-height: 500px; overflow-x: hidden; overflow-y: auto;">';
    //场景名
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>生成数量：</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<input type="text" class="input-text" placeholder="输入数量" value="10" onblur="formArys.num=this.value;"/>';
    formArys.num = 10;
    h += '</div>';
    h += '</div>';
    //按钮
    h += '<div class="row cl">';
    h += '<div class="col-xs-8 col-sm-9 col-xs-offset-3 col-sm-offset-2">';
    h += '<button class="btn btn-default radius" type="button" onClick="$(\'#formPop\').html(\'\');">&nbsp; 取 消 &nbsp;</button> <button class="btn btn-secondary radius" type="button" onClick="addOne();"><i class="Hui-iconfont">&#xe632;</i> 保 存 </button>';
    h += '</div>';
    h += '</div>';
    h += '</form>';
    h += '</div>';
    h += '</div>';
    h += '';
    $('#formPop').html(h);
}


//新增
function addOne(){
    if (formArys.num == 0) {
        msgPop({msg: '数量必须的'}); return
    }
    var formData = {
        aid: getAid(),
        num: formArys.num
    };
    console.log(formData);
    $.ajax({
        type: apis.qrAdd.method,
        url: domain + apis.qrAdd.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                //msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '添加成功'});
            window.location.href = '';
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//二维码页面-生成二维码
function popQr(v){
    var id = v;
    var qrlink = $('#qrlink-' + v).val();
    var showCode = $('#showCode-' + v).val();
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var cwBody = 220, chBody = 240;
    var cwTop = (ch - chBody) / 2 - 50, chLeft = (cw - cwBody) / 2;
    var h = '', i;
    h += '<style type="text/css">';
    h += '#qrMask{width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.5; position: fixed; top: 0; left: 0; z-index: 100;}';
    h += '#qrDiv{padding: 10px; width: ' + cwBody + 'px; height: ' + chBody + 'px; background: #FFF; border: 1px solid #DDD; box-shadow: 0 0 10px #000; overflow: hidden; position: fixed; top: '+cwTop+'px; left: '+chLeft+'px; z-index: 100;}';
    h += '.qrOne{margin-bottom: 10px; padding: 10px; width: 235px; height: 260px; float: left; position: relative;}';
    //h += '.qrtitle{padding-bottom: 10px; width: 200px; text-align: center; color: #000; font-weight: bolder; font-size: 20px;}';
    h += '.qrBox{width: 200px; height: 200px;}';
    h += '.qrtxt{padding-top: 5px; width: 200px; text-align: center; color: #000; font-weight: bolder; font-size: 18px;}';
    h += '#qrBtn{padding: 10px 1px; width: '+ (cwBody + 20) +'px; text-align: center; background: #fff; color: #333; cursor: pointer; position: fixed; top: ' + (cwTop + 280 ) + 'px; left: ' + (cw - cwBody) / 2 +'px; z-index: 100;}';
    h += '#qrBtn:hover{background: #ddd;}';
    h += '</style>';
    h += '<div id="qrMask" onclick="$(\'#qrHtml\').html(\'\');"></div>';
    h += '<div id="qrDiv">';
    h += '<div class="qrOne">';
    //h += '<div class="qrtitle">扫码有惊喜</div>';
    h += '<div class="qrBox" title="' + qrlink + '" id="qr' + id + '"></div>';
    h += '<div class="qrtxt">' + showCode + '</div>';
    h += '</div>';
    h += '</div>';
    h += '<div id="qrBtn" onclick="downQr(' + id + ');" title="' + qrlink + '"><b>点击下载二维码</b></div>';
    h += '<input type="hidden" id="showCode' + id + '" value="' + showCode + '"/>';
    $('#qrHtml').html(h);
    $('#qr'+id).qrcode({
        render: 'canvas', text: qrlink,
        width: 200, height: 200,
        background: "#fff", //背景颜色
        foreground: "#000", //前景颜色
        //src: '/imgs/baim.png'
    });
}


//点击二维码下载图片
function downQr(id){
    var ele = document.getElementById('qrDiv'),
        showCode = $('#showCode' + id).val();
    //console.log(ele, showCode); return
    html2canvas(ele,{
        useCORS: true,
        sale: 1,
    }).then(function(canvas){
        canvas.toBlob(function(blob){
            saveAs(blob, showCode + '.png');
        });
    });
}